package springboot.controller;

import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springboot.entity.Teacher;
import springboot.service.TeacherService;

/**
 * @author Sam
 * @since 3.0.0
 */
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TeacherController {

  private static Logger log = LoggerFactory.getLogger(TeacherController.class);

  @Resource
  private TeacherService teacherService;

  @RequestMapping(value = "/teacher/{tno}", method = RequestMethod.GET)
  @ResponseBody
  public ResponseEntity queryCustomerById(@PathVariable Long tno) {
    try {
      Teacher teacher = teacherService.queryById(tno);
      if (null == teacher) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
      }
      return ResponseEntity.ok(teacher);
    } catch (Exception e) {
      log.error("Query customer error", e);
      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
  }

  /**
   *  入参示例："吴悠"
   */
  @ResponseBody
  @RequestMapping(value = "/teacher/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity createTeacher(@RequestBody String name) {
    try {
      Teacher newTeacher = new Teacher();
      newTeacher.setTname(name);
      teacherService.createTeacher(newTeacher);
      return ResponseEntity.ok(newTeacher);
    } catch (Exception e) {
      log.error("Create teacher error", e);
      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
  }
}
